package com.pactera.miyuangroup.controller.app;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.pactera.miyuangroup.db.entity.*;
import com.pactera.miyuangroup.db.enums.AuditStatus;
import com.pactera.miyuangroup.filter.CurrentUser;
import com.pactera.miyuangroup.service.*;
import com.pactera.miyuangroup.vo.convertor.GoodsClassificationConvert;
import com.pactera.miyuangroup.vo.convertor.VendorInfoConvert;
import com.pactera.miyuangroup.vo.request.VoidRequest;
import com.pactera.miyuangroup.vo.request.app.AppPageVenderGoodsReq;
import com.pactera.miyuangroup.vo.request.app.AppPageVenderReq;
import com.pactera.miyuangroup.vo.request.app.AppUpdateVendorInfo;
import com.pactera.miyuangroup.vo.response.ResultModel;
import com.pactera.miyuangroup.vo.response.ResultStatusCode;
import com.pactera.miyuangroup.vo.response.User;
import com.pactera.miyuangroup.vo.response.VoidResponse;
import com.pactera.miyuangroup.vo.response.app.AppClassificationRes;
import com.pactera.miyuangroup.vo.response.app.AppPageVenderGoodsRes;
import com.pactera.miyuangroup.vo.response.app.AppPageVenderRes;
import com.pactera.miyuangroup.vo.response.app.AppVendorInfoRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 售货机
 *
 * @author Wangwu
 * @create 2018-04-18 下午5:14
 **/
@Api(description = "售货机信息接口")
@RestController
@RequestMapping("/app/vender")
public class VenderController {

    @Autowired
    private VendorInfoService vendorInfoService;
    @Autowired
    private GoodsClassificationService goodsClassificationService;
    @Autowired
    private GoodsClassificationConvert goodsClassificationConvert;
    @Autowired
    private VehicleInfoService vehicleInfoService;
    @Autowired
    private LoginUserService loginUserService;
    @Autowired
    private VendorInfoConvert vendorInfoConvert;
    @Autowired
    private EquipmentAuditService equipmentAuditService;

    @ApiOperation(value = "我的售货机列表", notes = "分页查询我的售货机信息列表")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ResultModel<Page<AppPageVenderRes>> selectVendorList(@CurrentUser User user, @RequestBody AppPageVenderReq request) {
        return ResultModel.ok(vendorInfoService.getAppVendorInfoList(user.getId(), request));
    }

    @ApiOperation(value = "售货机商品列表", notes = "分页查询售货机商品信息列表")
    @ApiImplicitParams({@ApiImplicitParam(value = "售货机ID", name = "vendorId", required = true, paramType = "path")})
    @RequestMapping(value = "/{vendorId}/goods", method = RequestMethod.POST)
    public ResultModel<Page<AppPageVenderGoodsRes>> selectVendorGoodsList(@PathVariable String vendorId, @RequestBody AppPageVenderGoodsReq request) {
        return ResultModel.ok(vendorInfoService.getAppVenderGoodsInfoList(vendorId, request));
    }

    @ApiOperation(value = "售货机商品分类查询", notes = "售货机商品分类查询")
    @RequestMapping(value = "/goods/classification", method = RequestMethod.POST)
    public ResultModel<List<AppClassificationRes>> selectVendorGoodsClassificationList(@RequestBody VoidRequest request) {
        List<GoodsClassification> list = goodsClassificationService.selectList(null);
        return ResultModel.ok(goodsClassificationConvert.convert2ClassificationResList(list));
    }


    @ApiOperation(value = "售货机详情查询", notes = "售货机详情查询")
    @ApiImplicitParams({@ApiImplicitParam(value = "售货机ID", name = "vendorId", required = true, paramType = "path")})
    @RequestMapping(value = "/{vendorId}/info", method = RequestMethod.GET)
    public ResultModel<AppVendorInfoRes> selectVendorInfo(@PathVariable String vendorId) {
        VendorInfo vendorInfo = vendorInfoService.selectById(vendorId);
        VehicleInfo vehicleInfo = vehicleInfoService.selectById(vendorInfo.getVehicleId());
        LoginUser user = loginUserService.selectById(vehicleInfo.getUserId());
        return ResultModel.ok(vendorInfoConvert.convert2AppVendorInfoRes(vendorInfo, user));
    }


    @ApiOperation(value = "修改售货机信息", notes = "修改售货机信息")
    @ApiImplicitParams({@ApiImplicitParam(value = "售货机ID", name = "vendorId", required = true, paramType = "path")})
    @RequestMapping(value = "/{vendorId}/update", method = RequestMethod.POST)
    public ResultModel<VoidResponse> updateVendorInfo(@PathVariable String vendorId, @CurrentUser User user, @RequestBody AppUpdateVendorInfo req) {
        EquipmentAudit equipmentAudit = new EquipmentAudit();
        //查询售货机是否已经提交过修改
        EntityWrapper ew = new EntityWrapper();
        ew.eq("vendor_id", vendorId);
        ew.eq("audit_status", AuditStatus.WAIT.getValue());
        int count = equipmentAudit.selectCount(ew);
        if (count > 0) {
            //已经提交申请
            return ResultModel.fail(ResultStatusCode.SUBMITTED_AUDITED);
        }
        equipmentAudit.setUserId(user.getId());
        equipmentAudit.setVendorId(vendorId);
        VendorInfo vendorInfo = vendorInfoService.selectById(vendorId);
        equipmentAudit.setBeforeVendorNumber(vendorInfo.getNumber());
        equipmentAudit.setAfterVendorNumber(req.getNumber());
        equipmentAudit.setRemarks(req.getRemarks());
        equipmentAudit.setAuditStatus(AuditStatus.WAIT);
        equipmentAudit.setAddTime(new Date());
        equipmentAudit.setNumbers(new com.pactera.miyuangroup.util.Date().toTimeStamp());
        boolean flg = equipmentAuditService.insert(equipmentAudit);
        if (!flg) {
            return ResultModel.fail();
        }
        return ResultModel.ok();
    }

}
